package com.iscale.controller;

import com.iscale.model.entity.BaseFileInfo;
import com.iscale.service.BaseFileInfoService;
import com.iscale.util.ResponseCode;
import com.iscale.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.iscale.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 文件信息 前端控制器
 * </p>
 *
 * @author Andychen
 * @since 2017-03-10
 */
@Api(value = "文件管理模块",description = "文件管理模块")
@Controller
@RequestMapping("/baseFileInfo")
public class BaseFileInfoController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(BaseFileInfoController.class);

    @Autowired
    BaseFileInfoService fileInfoService;

    @ApiOperation(value = "多文件上传", httpMethod = "POST", produces = "application/json", response = Result.class)
//    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "multFilesUpload", method = RequestMethod.POST)
    public Result multFilesUpload(@ApiParam(value = "多文件列表", required = true) @RequestParam(required = true) MultipartFile[] files) throws IOException {
        //String realPath =  request.getSession().getServletContext().getRealPath("/upload");
        List<BaseFileInfo> fileInfoList = fileInfoService.saveFileInfos(files,null);

        return Result.success(fileInfoList);
    }

    @ApiOperation(value = "单文件上传", httpMethod = "POST", produces = "application/json", response = Result.class)
//    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "singleFilesUpload", method = RequestMethod.POST)
    public Result singleFilesUpload(@ApiParam(value = "上传文件", required = true) @RequestParam(required = true) MultipartFile file) throws IOException {
        List<BaseFileInfo> fileInfoList = fileInfoService.saveFileInfos(new MultipartFile[]{file} ,"1001");
        if(null!=fileInfoList && fileInfoList.size()>0){
            return Result.success(fileInfoList.get(0));
        }else{
            return Result.error(ResponseCode.file_upload_error);
        }
    }

}
